﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class RoleExtraConfigProcessBll 
    {
        public static RoleExtraConfigCls CreateRoleExtraConfigModel(string SiteId, string RoleExtraConfigId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRoleExtraConfig " +
                                               "     where RoleExtraConfigId=@RoleExtraConfigId",
                    new DbParam[] 
                    { 
                        new DbParam("RoleExtraConfigId", RoleExtraConfigId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            RoleExtraConfigCls ORoleExtraConfig = RoleExtraConfigParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ORoleExtraConfig;
        }

        public static RoleExtraConfigCls CreateRoleExtraConfigModel(string SiteId, string AssetCode, string RoleId, string DataLanguageId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableRoleExtraConfig " +
                                               "     where AssetCode=@AssetCode and RoleId=@RoleId and frkDataLanguageId=@DataLanguageId",
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("RoleId", RoleId),
                        new DbParam("DataLanguageId", DataLanguageId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            RoleExtraConfigCls ORoleExtraConfig = RoleExtraConfigParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ORoleExtraConfig;
        }

        public static void SaveRoleExtraConfig(
            string SiteId, 
            string RoleId, 
            string DataLanguageId, 
            RoleExtraConfigCls ORoleExtraConfig)
        {
            if (string.IsNullOrEmpty(ORoleExtraConfig.RoleExtraConfigId)) ORoleExtraConfig.RoleExtraConfigId = System.Guid.NewGuid().ToString();
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string DelQuery = " Delete from TableRoleExtraConfig where RoleId=@RoleId and frkDataLanguageId=@DataLanguageId";
            DBService.ExecuteNonQuery(null, DelQuery, new DbParam[] { new DbParam("RoleId", RoleId), new DbParam("DataLanguageId", DataLanguageId) });
            DBService.Insert(null, "TableRoleExtraConfig",
                    new DbParam[] 
                    { 
                        new DbParam("RoleExtraConfigId", ORoleExtraConfig.RoleExtraConfigId),
                        new DbParam("RoleId",RoleId),
                        new DbParam("Categories",ORoleExtraConfig.Categories),
                        new DbParam("frkDataLanguageId",DataLanguageId),
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                    });
        }


    }
}

